package com.example.shoppinglist;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;

import com.neopixl.pixlui.components.edittext.EditText;
import com.qustom.dialog.QustomDialogBuilder;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

public class EditItemDialogFragment extends DialogFragment{

	private static final String EDIT_TEXT_NAME_STRING = "name";
	private static final String EDIT_TEXT_COST_STRING = "cost";
	private EditText editTextName;
	private EditText editTextCost;
	long itemId;
	int listId;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(R.layout.edit_item, null);
		editTextName = (EditText) view.findViewById(R.id.name_edit);
		editTextCost = (EditText) view.findViewById(R.id.cost_edit);

		//getting relevant items
		itemId = getArguments().getLong(ShoppingListFragment.ITEM_ID_KEY);
		listId = getArguments().getInt(ShoppingListFragment.LIST_ID_KEY);
		String itemName = getArguments().getString(ShoppingListFragment.ITEM_NAME_KEY);

		Drawable icon = getResources().getDrawable(R.drawable.ic_launcher);
		int color = getResources().getColor(R.color.drawer_background);

		AlertDialog dialog = new QustomDialogBuilder(getActivity())
		.setTitle("Edit " + itemName)
		.setTitleColor(color)
		.setIcon(icon)
		.setDividerColor(color)
		.setCustomView(view)
		.setPositiveButton("OK", new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String name = editTextName.getText().toString();
				String cost = editTextCost.getText().toString();
				if(!name.trim().isEmpty()){
				DecimalFormat df = new DecimalFormat("#0.00");
				df.setParseBigDecimal(true);
				try {
					if(cost.trim().isEmpty()){
						((ShoppingListFragment) getTargetFragment()).editName(itemId, name);
					}else{
						((ShoppingListFragment) getTargetFragment()).editNameAndValue(itemId, name, (BigDecimal) df.parse(cost));
					}
				} catch (ParseException e) {
					throw new NumberFormatException(e.getMessage());
				}
			}
			}
		})
		.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		})
		.create();

		dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		return dialog;
	}

	@Override
	public void onSaveInstanceState(Bundle arg0) {
		//saving the edit text
		arg0.putString(EDIT_TEXT_NAME_STRING, editTextName.getText().toString());
		arg0.putString(EDIT_TEXT_COST_STRING, editTextCost.getText().toString());
		super.onSaveInstanceState(arg0);
	}
}
